package com.stp.rabbitmq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * Queue与Exchange指定BindingKey可以使用通配符：
 *
 * #：代指0个或多个单词
 *
 * *：代指一个单词
 *
 * 比如：
 *
 * bindingkey: china.# ->中国的所有消息
 *
 * bindingkey: #.weather ->所以国家的天气
 */
@Component
public class TopicRabbitmqListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "TopicQueue1"),
            exchange = @Exchange(name = "TopicExchange",type = ExchangeTypes.TOPIC),
            key = {"china.#"}
    ),ackMode = "MANUAL")
    public void listenTopicQueue1(String msg){
        System.out.println("listenTopicQueue1接收到的消息："+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "TopicQueue2"),
            exchange = @Exchange(name = "TopicExchange",type = ExchangeTypes.TOPIC),
            key = {"#.news"}
    ),ackMode = "MANUAL")
    public void listenTopicQueue2(String msg){
        System.out.println("listenTopicQueue2接收到的消息："+msg);
    }
}
